package android.vstore.activities;


import java.io.Console;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import DAO.NotificationDAO;
import DTO.TopicDTO;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.SearchManager;
import android.app.ActionBar.OnNavigationListener;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SyncStateContract.Constants;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.vstore.custom.AppSectionsPagerAdapter;
import android.vstore.fragment.PageSectionCatergoryFragment;
import android.vstore.fragment.PageSectionFragment;
import android.vstore.fragment.PageVideoFragment;
import android.widget.FrameLayout;
import android.widget.PopupMenu;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.PopupMenu.OnMenuItemClickListener;
import anroid.vstore.provider.Constant;
import anroid.vstore.provider.SharedPreference;

import com.appvn2014.appstorevn.R;
import com.viewpagerindicator.TabPageIndicator;

@SuppressLint("NewApi")
public class SceneGroupVideoActivity extends BaseActivity implements ActionBar.TabListener, OnNavigationListener, OnMenuItemClickListener {

	
	public static final int ReLoad = 1000; // flag refresh page
	int _type ;
	 
	
	// Views
	
	private ArrayList<Fragment> lstFrags;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scene_group);

		InitActionBar();
		
		
		
		lstFrags = new ArrayList<Fragment>();
		lstFrags.add(PageVideoFragment.newInstance(this,0,Constant.VIDEO_HOT,_messageHandler));
		lstFrags.add(PageVideoFragment.newInstance(this,1,Constant.VIDEO_NEW,_messageHandler));
		lstFrags.add(PageVideoFragment.newInstance(this,2,Constant.VIDEO_VIEWS,_messageHandler));
		
		
		mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager(),lstFrags);



        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });
        
        
        TabPageIndicator titleIndicator = (TabPageIndicator)findViewById(R.id.indicator);
		titleIndicator.setViewPager(mViewPager);
        InitTitle();
        
        mViewPager.setCurrentItem(1);
	}
	
	void InitTitle()
	{
		if(actionBar != null)
			actionBar.setTitle(R.string.label_video);
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		invalidatePages();
	}
	private void invalidatePages() {
		for(int i =0; i < lstFrags.size(); i++)
		{
			if(lstFrags.get(i).getClass().equals(PageSectionFragment.class))
			{
				((PageSectionFragment)lstFrags.get(i))._messageHandler.sendMessage(Message.obtain(_messageHandler, Constant.REFRESH_ALL_PAGE, 0, 0, null));
			}

		}
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        case R.id.action_settings:
        	showMenu(findViewById(R.id.action_settings));
        	return true;
        }
        return super.onOptionsItemSelected(item);
    }
	public void showMenu(View v) {
	    PopupMenu popup = new PopupMenu(this, v);

	    // This activity implements OnMenuItemClickListener
	    popup.setOnMenuItemClickListener(this);
	    popup.inflate(R.menu.maincatergories);
	    popup.show();
	}

	public void InterruptAll()
	{
		
	}
	@Override
	public boolean onMenuItemClick(MenuItem item) {
		actionBar.setTitle(item.getTitle());
	    switch (item.getItemId()) {
	        case R.id.menu_all:	  
	        	_type = -1;
	        	Intent resultIntent = new Intent();
	        	setResult(Constant.TYPE_ALL, resultIntent);
	        	finish();
	            break;
	        case R.id.menu_app:
	        	_type = Constant.TYPE_APPLICATION;
	        	break;
	        case R.id.menu_game:
	        	_type = Constant.TYPE_GAME;
	        	break;
	        default:
	            return false;
	    }
	    for(int i =0; i < lstFrags.size(); i++)
		{
			if(lstFrags.get(i).getClass().equals(PageSectionFragment.class))
			{
				((PageSectionFragment)lstFrags.get(i)).setType(_type);
			}
			else
			{
				((PageSectionCatergoryFragment)lstFrags.get(i)).setType(_type);
			}
		}
	    return true;
	    
	}
	 /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter}
     * derivative, which will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will display the three primary sections of the app, one at a
     * time.
     */
    ViewPager mViewPager;

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
    
    @Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		// TODO Auto-generated method stub
		return false;
	}
   
  
	
}
